import pygame
# 定义一个马里奥的精灵类


class Player(pygame.sprite.Sprite):
    def __init__(self, name):
        pygame.sprite.Sprite.__init__(self)
        self.time = 0
        # 面朝左边吗？ 默认是假
        self.left = False
        # 跑步的帧
        self.run_index = 0
        # 定义当前的状态,默认是站立
        self.current_state = "standing"
        # 加载图片
        self.load_images()

    # 加载图片的方法
    def load_images(self):
        # 读取马里奥素材图片
        file = pygame.image.load("./img/graphics/mario_bros.png")
        # 存放所有的帧
        self.stand_frame = []
        self.run_frame = []
        self.jump_frame = []
        # 站立姿态
        self.stand_frame.append(self.cut_image(file, 178, 32, 12, 16))
        # 跑步1
        self.run_frame.append(self.cut_image(file, 80, 32, 15, 16))
        # 跑步2
        self.run_frame.append(self.cut_image(file, 96, 32, 16, 16))
        # 跑步3
        self.run_frame.append(self.cut_image(file, 112, 32, 15, 16))
        # 跳跃姿态
        self.jump_frame.append(self.cut_image(file, 144, 32, 15, 16))
        # 设置初始图片为站立姿态
        self.image = self.stand_frame[0]
        # 初始化精灵的rect位置
        self.rect = self.image.get_rect()
        pass

    # 精灵类主要的更新方法
    def update(self, keys):

        # 如果按下了左键
        if keys[pygame.K_LEFT]:
            # 左移
            self.rect.x -= 5
            self.left = True
            self.current_state = "running"
        # 如果按下了右键
        elif keys[pygame.K_RIGHT]:
            # 右移
            self.rect.x += 5
            self.left = False
            self.current_state = "running"
        # 如果按下了空格
        elif keys[pygame.K_SPACE]:
            # 跳跃
            self.jump()
            self.current_state = "jumping"
            
        else:
            self.current_state = "standing"
            
        # 帧的更新
        self.update_frame()

    # 定义帧更新的方法
    def update_frame(self):
        if(self.current_state == "standing"):
            self.update_stand_frame()
        if(self.current_state == "running"):
            self.update_run_frame()
        if(self.current_state == "jumping"):
            self.update_jump_frame()
        pass

    # 跑步的帧更新
    def update_run_frame(self):
        current_time = pygame.time.get_ticks()
        
        if(self.time == 0):
            self.time = current_time
        elif(current_time - self.time > 200):
            self.time = current_time
            self.count_run_index()
            temp = self.run_frame[self.run_index-1]
            
            if(self.left):  
                # 如果朝左，就要镜像翻转图片
                temp = pygame.transform.flip(temp,True,False)
            self.image = temp

    # 还是之前的数数012/012的方法   
    def count_run_index(self):
        self.run_index = self.run_index+1
        if(self.run_index == 3):
            self.run_index = 0
        

    # 站立的帧更新
    def update_stand_frame(self):
        # 站立就是第0帧
        self.image = self.stand_frame[0]

    # 跳跃操作
    def jump(self):
        pass

    # 跳跃的帧更新
    def update_jump_frame(self):
        pass

    # 裁切图片（图片，裁切目标左上角x，裁切目标左上角y，裁切目标宽度，裁切目标高度）
    def cut_image(self, source, x, y, width, height):
        image = pygame.Surface([width, height])
        image.blit(source, (0, 0), (x, y, width, height))
        image.set_colorkey((0, 0, 0))
        image = pygame.transform.scale(image, (width*3, height*3))
        return image

    # 把自己画到屏幕上   
    def draw(self, screen):
        screen.blit(self.image, (self.rect.x, self.rect.y)) 